import { combineReducers } from 'redux'
import data from './data'
// 按钮权限

const role_button = (state = data.role_button, action) => {
    switch (action.type) {
        case 'set': //缓存用户信息
            data.setStorage('role_button', action.data)
            return action.data
        case 'logout':
            data.clearStorage()
            return state
        default:
            return state
    }
}
// 用户信息
const user = (state = data.userInfo, action) => {
    const name = 'user'
    switch (action.type) {
        case 'set': //缓存用户信息
            data.setStorage(name, action.data)
            return action.data
        case 'logout':
            data.clearStorage()
            return state
        default:
            return state
    }
}
// 全局权限菜单
const menus = (state = data.menus, action) => {
    switch (action.type) {
        case 'setMenus':
            sessionStorage.setItem('menus', JSON.stringify(action.data))
            return action.data
        default:
            return state || []
    }
}
const initState = false;
const loadStatus = (state = initState, action) => {
    switch (action.type) {
        case 'OPENPAGELOADING': {
            return true
        }
        case 'CLOSEPAGELOADING': {
            return false
        }
        default: {
            return state
        }
    }
}
// 用户按钮权限
const ident = (state = data.ident, action) => {
    const name = 'ident'
    switch (action.type) {
        case 'setIdent': //缓存用户信息
            sessionStorage.setItem(name, action.data)
            return action.data
        default:
            return state
    }
}
const searchValue = (state = data.searchValue, action) => {
    switch (action.type) {
        case 'ClearSearchValues': //清楚搜索信息
            sessionStorage.removeItem('searchValue', {})
            return {}
        case 'SaveSearchValues': //缓存搜索信息
            localStorage.setItem('searchValue', JSON.stringify(action.data))
            return action.data
        default:
            return state
    }
}
const broadcast = (state = data.broadcast, action) => {

    switch (action.type) {
        case 'OpenBroadcast': //清楚搜索信息
            // state.
            var obj = document.createElement("embed");
            obj.style.width = 0;
            obj.style.height = 0;
            obj.src = "http://tsn.baidu.com/text2audio?lan=zh&cuid=9C-4E-36-43-DB-D0&ctp=1&vol=9&tok=24.5760f9c29a27f22f21fd0e9410ffbef0.2592000.1505287074.282335-10005162&tex=" + encodeURI(encodeURI(action.data));
            obj.loop = 0;
            document.body.appendChild(obj)
            return action.data
        case 'CloseBroadcast': //缓存搜索信息

            return action.data
        default:
            return state
    }
}
// 输出对应的state
export default combineReducers({
    user,
    menus,
    loadStatus,
    ident,
    searchValue,
    broadcast
})
